﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Controls;

using HA.Data;

using HA.ModFlow.Domain.DomainLayer;


namespace HA.ModFlow.Application
{

   public class ModFlowViewModel:MDTVIViewModel
    {

           public ModFlowViewModel(MDTVIViewModel parent,ModFlowData mfd):base(parent,false)
           {


               _modflowData = mfd;


               this._imageSource = VMImageSource.modflow;

               this._contextMenu = new System.Windows.Controls.ContextMenu();

               MenuItem mi = new MenuItem();
               mi.Header = "新建Modflow";
               mi.Command = DataCommands.AddModflow;
               

               this._contextMenu.Items.Add(mi);


                mi = new MenuItem();
               mi.Header = "删除Modflow";
               mi.Command = DataCommands.RemoveModflow;

               this._contextMenu.Items.Add(mi);

               mi = new MenuItem();
               mi.Header = "校验Modflow";
               mi.Command = HA.ModFlow.Controls.AppCommands.CheckModflow;

               this._contextMenu.Items.Add(mi);



               mi = new MenuItem();
               mi.Header = "运行Modflow";
               mi.Command = HA.ModFlow.Controls.AppCommands.RunModFlow;

               this._contextMenu.Items.Add(mi);

               //mi = new MenuItem();
               //mi.Header = "Display Option...";
               //this._contextMenu.Items.Add(mi);
               //Separator sep = new Separator();
               //this._contextMenu.Items.Add(sep);

               //mi = new MenuItem();
               //mi.Header = "Convergence Option...";
               //this._contextMenu.Items.Add(mi);

               //mi = new MenuItem();
               //mi.Header = "Parameters...";
               //this._contextMenu.Items.Add(mi);

               //mi = new MenuItem();
               //mi.Header = "Observations...";
               //this._contextMenu.Items.Add(mi);

               //mi = new MenuItem();
               //mi.Header = "Parameter Estimation...";
               //this._contextMenu.Items.Add(mi);

               //mi = new MenuItem();
               //mi.Header = "Stochastic...";
               //this._contextMenu.Items.Add(mi);
              





               this.LoadChildren();


           }


           ModFlowData _modflowData;

           public ModFlowData ModflowData
           {
               get { return _modflowData; }
               set { _modflowData = value; }
           }




           public override string Desc
           {
               get
               {
                   return "MODFLOW模型";
               }
           }


           LPFMFViewModel _vmLpf;
           BCFMFViewModel _vmBcf;


           protected override void LoadChildren()
           {


               GlobalMFViewModel global = new GlobalMFViewModel(this,this._modflowData.GridData);

               this.Children.Add(global);


               if (this._modflowData.FlowData != null)
               {

                   this._vmLpf = new LPFMFViewModel(this, this._modflowData.FlowData.LpfData);

                   this._vmBcf = new BCFMFViewModel(this, this._modflowData.FlowData.BcfData);

                   if (this._modflowData.ModelOption.InnerPack == INNERPACK.LPF)
                   {


                       this.Children.Add(this._vmLpf);

                   }

                   else
                   {
                       this.Children.Add(this._vmBcf);
 
                   }


               }



             








               if (ProjectService.DefaultService.Project.Mt3dmsData!=null)
               {
                   MT3DMSViewModel vm = new MT3DMSViewModel(this, ProjectService.DefaultService.Project.Mt3dmsData);

                   this.Children.Add(vm);



                   
               }

               ProjectService.DefaultService.Project.MT3dMSChanged += new EventHandler<ModelChangedEventArgs>(Project_MT3dMSChanged);

               ProjectService.DefaultService.Project.MfData.ModelOption.InnerPackChanged += new EventHandler(ModelOption_InnerPackChanged);

            
           }


       /// <summary>
       /// 替换对应的子项
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>


           void ModelOption_InnerPackChanged(object sender, EventArgs e)
           {

               if (this._modflowData.ModelOption.InnerPack == INNERPACK.LPF)
               {


                   this.Children.Remove(this._vmBcf);

                   this.Children.Add(this._vmLpf);

               }

               else
               {

                   this.Children.Remove(this._vmLpf);

                   this.Children.Add(this._vmBcf);

               }



           }

           void Project_MT3dMSChanged(object sender, ModelChangedEventArgs e)
           {
               switch (e.ChangedType)
               {
                   case ModelChangedType.CRREATED:

                     MT3DMSViewModel vm = new MT3DMSViewModel(this, ProjectService.DefaultService.Project.Mt3dmsData);

                    this.Children.Add(vm);


                       break;
                   case ModelChangedType.DELETED:
                       MT3DMSViewModel mt = this.GetMT3DViewModel();

                       if (mt!=null)
                       {
                           this.Children.Remove(mt);
                           
                       }

                       //this.Children.RemoveAt(0);
                       break;
                   default:
                       break;
               }
           }



           protected MT3DMSViewModel GetMT3DViewModel()
           {
               foreach (MDTVIViewModel item in this.Children)
               {
                   if (item is MT3DMSViewModel)
                   {
                       return (MT3DMSViewModel)item;
                       
                   }
                   
               }

               return null;


           }



           protected override void OnCheckedChanged()
           {
               string name = GisModule.DefaultModule.ModFlowLayerId;

               GisModule.DefaultModule.SetLayerVisible(name,this.IsChecked);
             
           }

           protected override void OnSelectedChanged()
           {
               if (this.IsSelected)
               {

                  



                

                   UIModule.DefaultModule.RibbonV2ViewModel.ViewOpViewModel.ToolVM.CurrentTool = APPTOOLMODE.GRID3D;


               }

           }






    }
}
